package zjut.raymond.witdemo.sensor;

import java.util.ArrayList;
import java.util.List;

public class SensorData implements MyObserverAble {
    private List<SensorDataObserver> observers;
    private MyEvent event;

    public SensorData() {
        observers = new ArrayList<>();
    }

    public void setEvent(MyEvent event) {
        this.event = event;
        notifyObservers();
    }

    @Override
    public void registerObserver(SensorDataObserver observer) {
        if (observers.contains(observer)){
            return;
        }
        observers.add(observer);
    }

    @Override
    public void removeObserver(SensorDataObserver observer) {
        int i = observers.indexOf(observer);
        if (i >= 0) {
            observers.remove(i);
        }
    }

    @Override
    public void notifyObservers() {
        for (SensorDataObserver o :
                observers) {
            o.onSensorChanged(event);
        }
    }
}
